import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.io.*;
import javax.sound.sampled.*;

public class ViewPanelMenu extends AbstractView{


	private PanelMenu panelMenu;
	private ControllerMenu controller;
	private Image backGround;
	
	private boolean bgmFlag;
	private Thread bgm;
	
	/*
	 * Class PanelMenu implements the panel 
	 */
	public class PanelMenu extends JPanel{		
		/*
		 * Inner class constructor
		 */
		public PanelMenu() {
			setLayout(null);
			addComponents();
		}

		public void paintComponent(Graphics g) {
	        Graphics2D g2D = (Graphics2D) g;
			backGround = Toolkit.getDefaultToolkit().getImage("./src/Images/OthelloMenuImage.jpg");

	        /*
	         * Draw background image
	         */
			double imageWidth = 899;
	        double imageHeight = 599;
	        double panelWidth = this.getWidth();
	        double panelHeight = this.getHeight();

	        double sx = (panelWidth / imageWidth);
	        double sy = (panelHeight / imageHeight);
	        g.clearRect(0, 0, this.getWidth(),this.getHeight());
	        AffineTransform af = AffineTransform.getScaleInstance(sx, sy);
	        g2D.drawImage(backGround, af, this);
	        
	        
	        g2D.setColor(Color.ORANGE);
	        g2D.setFont(new Font("Arial", Font.BOLD, 20));
		}
		
		/*
	     * Initialize the basic screen
	     */
		private void addComponents() {		
			/*
			 * Clear the panel
			 */
			this.removeAll();			
			
	        /*
	         * Initialize the buttons
	         */
	        JButton buttonSinglePlayer = new JButton("Single Player");
	        JButton buttonMultiPlayers = new JButton("Multiple Players");
	        JButton buttonOptions = new JButton("Options");
	        JButton buttonExit = new JButton("Exit");
	        
	        buttonSinglePlayer.setBounds((ViewFrameMain.PANEL_PIXEL_WIDTH)/2-95, 150, 190, 50);
	        buttonMultiPlayers.setBounds((ViewFrameMain.PANEL_PIXEL_WIDTH)/2-95, 250, 190, 50);
	        buttonOptions.setBounds((ViewFrameMain.PANEL_PIXEL_WIDTH )/2-95, 350, 190, 50);
	        buttonExit.setBounds((ViewFrameMain.PANEL_PIXEL_WIDTH)/2-95, 450, 190, 50);
	        
	        /*
	         * Add ActionListener to buttons
	         */
	        buttonSinglePlayer.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent evt) {
	            	bgmFlag = false;
	            	System.out.println();
	            	controller.buttonPressedSinglePlayer();
	            }
	        });
	        
	        buttonMultiPlayers.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent evt) {
	            	bgmFlag = false;
	            	System.out.println();
	            	controller.buttonPressedMultiPlayers();
	            }
	        });
	        
	        buttonOptions.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent evt) {
	            	System.out.println();
	            	controller.buttonPressedOptions();
	            }
	        });
	        
	        buttonExit.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent evt) {
	            	System.out.println();
	            	controller.buttonPressedExit();
	            }
	        });
	        
	        /*
	         * Add buttons to the menu
	         */
	        this.add(buttonSinglePlayer);
	        this.add(buttonMultiPlayers);
	        this.add(buttonOptions);
	        this.add(buttonExit);
	        
	        /*
			 * Refresh
			 */
	        this.repaint();
		}
	}
	
	
	/*
	 * Runnable Class for BGM
	 */
	public class BGM implements Runnable{
		public void run(){
			// Data received
			byte[] buff = new byte[160];
			// Data format
			AudioFormat af = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, //Audio Encoding Method
					44100.0F, // Sampling number per 1sec (need to fits the import data)
					16, // bits per sample
					2, // channels (2 = stereo)
					4, // bytes per frame
					480000.0F, // frame per 1sec
					false); // ??? false is little endian.
			// Data line Object
			DataLine.Info info = new DataLine.Info(SourceDataLine.class,af);
			// Get the data line by using created line
			SourceDataLine line = null;

			try{
				line = (SourceDataLine)AudioSystem.getLine(info);
				line.open(af);
			}catch(Exception e){
				System.out.println("Error when audio output setting" + e);
				return ;
			}
			// Start output the data
			line.start();

			try{
				BufferedInputStream reader = new BufferedInputStream(new FileInputStream("./src/BGMs/menuBGM.wav"));

				  
				reader.read(buff);
				if(reader.markSupported())
					reader.mark(4262405);	// Apparently, the size needs to be 1 byte bigger than the Input file to mark the whole file.

				while(bgmFlag){
					
					if(reader.read(buff)!=-1){
						line.write(buff,0,buff.length);
					}
					else
						reader.reset();
				}
				line.stop();
				line.close();
				reader.close();
			}catch(Exception e){
				e.printStackTrace();
			}
			
		}
		
	}
	/*
	 * Constructor
	 */
	public ViewPanelMenu() {
		panelMenu = new PanelMenu();
		bgm = new Thread(new BGM());
		bgmFlag = true;
		bgm.start();		
	}

	/*
	 * Accessors
	 */
	public JPanel getPanel() {
		return panelMenu;
	}
	
	public ControllerMenu getController() {
		return controller;
	}
	
	/*
	 * Mutators
	 */
	public void setController(ControllerMenu ctrl) {
		controller = ctrl;
	}

}
